// pages/binding/binding.js
const app = getApp()
var interval
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '请选择日期',
    fun_id: 2,
    time: '获取验证码',
    currentTime: 60,
    code: '',
    mobile: '',
    lock:false,
  },
  //发送验证码
  getCode: function (options) {
    var that = this;
    var phone = that.data.phone

    if (phone == '' || phone == undefined){
      wx.showToast({
        title: '手机号不能为空',
      })
      return false
    }
    if(!that.data.lock){
      wx.request({
        url: app.data.Url + 'User/checkcode',
        data: {
          phone: phone
        },
        method: 'get',
        success: function (res) {
          console.log(res)
          that.setData({
            code: res.data
          })
        }
      })
    }
      
    var currentTime = that.data.currentTime
    interval= setInterval(function () {
      that.data.lock = true
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 60,
          disabled: false,
          lock:false
        })
      }
    }, 1000)
  },
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },

  //手机号
  phone:function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  //验证码
  coke: function (e) {
    this.setData({
      coke: e.detail.value
    })
  },


  mobileInput: function (e) {
  
    var that = this
    this.setData({
      mobile: e.detail.value
    })
    wx.request({
      url: app.data.Url + 'User/checkcode',
      data: {
        phone: e.detail.value
      },
      method: 'get',
      success: function (res) {
      
        that.setData({
          code: res.data
        })
      }
    })
  },
  btnclick: function () {
    let that = this;
    var phone = that.data.phone;
    var coke = that.data.coke;
    var code = that.data.code;
    if (phone == '' || phone == undefined) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return false
    }
  
    if (coke == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return false
    }
   
    if (code != coke ){
      wx.showToast({
        title: '验证码错误',
        icon:'none'
      })
      return false;
    }
    wx.request({
      url: app.data.Url + 'system/changephone',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        phone: that.data.phone,
        phoney: that.data.oldphone
      },
      method: 'post',
      success: function (res) {
        if (res.data.code==1){
          wx.setStorageSync('phone', that.data.phone);
          wx.showToast({
            title: '绑定成功！',
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/mine/mine',
                })

              },1000)
            }
          })
          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      oldphone: options.oldphone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // if(wx.getStorageSync('phone')!=''){
    //   wx.showToast({
    //     title: '您已经绑定手机号',
    //     duration:1000,
    //     icon:'none',
    //     success:function(){
    //       setTimeout(function(){
    //         wx.navigateBack({

    //         })
    //       },1000)
    //     }
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})